package cn.tedu.tea.service;

import cn.tedu.tea.domain.OrderGoods;
import cn.tedu.tea.domain.param.OrderGoodsAddNewDTO;
import cn.tedu.tea.domain.param.OrderGoodsUpdateDTO;
import cn.tedu.tea.domain.vo.OrderGoodsListVO;
import cn.tedu.tea.domain.vo.OrderGoodsStandardVO;
import cn.tedu.tea.ex.ServiceException;
import cn.tedu.tea.mapper.OrderGoodsMapper;
import cn.tedu.tea.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class OrderGoodsServiceImpl implements IOrderGoodsService {

    @Autowired
    private OrderGoodsMapper orderGoodsMapper;

    @Override
    public void addNew(OrderGoodsAddNewDTO orderGoodsAddNewDTO) {
        log.debug("开始处理【添加订单商品】的业务，参数： {}", orderGoodsAddNewDTO);
        int count = orderGoodsMapper.countByOrderNo(orderGoodsAddNewDTO.getOrderNo());
        if (count > 0){
            String message = "添加订单商品失败，系统文件名已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        OrderGoods orderGoods = new OrderGoods();
        BeanUtils.copyProperties(orderGoodsAddNewDTO, orderGoods);
        int rows = orderGoodsMapper.insert(orderGoods);
        if (rows != 1){
            String message = "添加订单商品失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Integer id) {
        log.debug("开始处理【根据id删除订单商品】的业务，参数： {}", id);
        OrderGoodsStandardVO orderGoodsStandard = orderGoodsMapper.getStandardById(id);
        if (orderGoodsStandard == null){
            String message = "删除订单商品失败，要删除的订单商品不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int rows = orderGoodsMapper.deleteById(id);
        if (rows != 1){
            String message = "删除订单商品失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void deleteByIds(Integer[] ids) {
        log.debug("开始处理【根据id批量删除订单商品】的业务，参数： {}", ids);
        int rows = orderGoodsMapper.deleteByIds(ids);
        if (rows < 1){
            String message = "删除订单商品失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(OrderGoodsUpdateDTO orderGoodsUpdateDTO) {
        log.debug("开始处理【修改订单商品信息】的业务，参数：{}", orderGoodsUpdateDTO);
        OrderGoodsStandardVO orderGoodsStandard = orderGoodsMapper.getStandardById(orderGoodsUpdateDTO.getId());
        if (orderGoodsStandard == null){
            String message = "修改订单商品失败，要访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int count = orderGoodsMapper.countByOrderNoAndNotId(orderGoodsUpdateDTO.getOrderNo(), orderGoodsUpdateDTO.getId());
        if (count > 0){
            String message = "修改订单商品失败，系统文件名已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        OrderGoods orderGoods = new OrderGoods();
        BeanUtils.copyProperties(orderGoodsUpdateDTO, orderGoods);
        int rows = orderGoodsMapper.update(orderGoods);
        if (rows != 1){
            String message = "修改订单商品失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public OrderGoodsStandardVO getStandardById(Integer id) {
        log.debug("开始处理【根据id查询订单商品详情】的业务，参数： {}", id);
        OrderGoodsStandardVO orderGoodsStandard = orderGoodsMapper.getStandardById(id);
        if (orderGoodsStandard == null){
            String message = "查询订单商品详情失败，要查询的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return orderGoodsStandard;
    }

    @Override
    public List<OrderGoodsListVO> list() {
        log.debug("开始处理【查询订单商品列表】的业务，无参数");
        List<OrderGoodsListVO> list = orderGoodsMapper.list();
        return list;
    }
    
}
